<?php
namespace app\api\controller\v1;

use \app\common\controller\ApiCommon;
use \app\common\model\CourseRoom;
use \think\Db;

class CoursesRoom extends ApiCommon
{
    protected $modelName = 'CourseRoom';

    /**
     * 索引课程信息
     *
     * @return void
     */
    public function index()
    {
        $this->checkPermission(10);
        return parent::_index();
    }

    /**
     * 读取单个课程信息
     *
     * @param [type] $id
     * @return void
     */
    public function read($id)
    {
        $this->checkPermission(10);
        return parent::_read($id);
    }

    /**
     * 删除单个课程信息
     *
     * @param [type] $id
     * @return void
     */
    public function delete($id)
    {
        $this->checkPermission(2);
        return parent::_delete($id);
    }

    /**
     * 删除多个课程信息
     *
     * @param [type] $ids
     * @return void
     */
    public function deletes($ids)
    {
        $this->checkPermission(2);
        return parent::_deletes($ids);
    }

    /**
     * 保存一个课程数据
     *
     * @return void
     */
    public function save()
    {
        $this->checkPermission(2);
        $request = $this->request;
        $param = $request->param();

        //在此处校验数据完整性
        if (!strlen($request->param('name', ''))) {
            return json_response([], STATUS_INVALD_PARAM, '必须传入课程名');
        }
        if (!isset($param['class_week']) && !$param['class_week']) {
            return json_response([], STATUS_INVALD_PARAM, '必须传入上课周次,且上课周次不能为0');
        }

        if (!isset($param['class_time']) && !$param['class_time']) {
            return json_response([], STATUS_INVALD_PARAM, '必须传入上课节次,且上课节次不能为0');
        }
        if (!isset($param['week']) && $param['week'] >= 1 && $param['week'] <= 7) {
            return json_response([], STATUS_INVALD_PARAM, '必须传入星期,且星期必须在1-7之间');
        }

        if (!isset($param['term_id'])) {
            return json_response([], STATUS_INVALD_PARAM, '必须传入学期ID');
        }

        if (!isset($param['room'])) {
            return json_response([], STATUS_INVALD_PARAM, '必须传入课室');
        }

        $model = $this->_getModel();
        $conflict = $model->where([
            ['term_id', '=', $param['term_id']],
            ['week', '=', $param['week']],
            ['room', '=', $param['room']],
            ['', 'exp', Db::raw('(class_week&' . $param['class_week'] . ')<>0 and (class_time&' . $param['class_time'] . ')<>0')],
        ])->column('id');

        if ($conflict) {
            return json_response($conflict, STATUS_INVALD_PARAM, '新传入的数据与原始数据存在冲突,data中存储了与此提交冲突的所有课程的id');
        }
        $newCourse = CourseRoom::create($param);
        return json_response([$newCourse->id]);
    }

    /**
     * 更新一个课程信息
     *
     * @param [type] $id
     * @return void
     */
    public function update($id)
    {
        $this->checkPermission(2);
        $request = $this->request;

        $model = $this->_getModel();
        $courseModel = $model->get($id);
        $courseModel->appendData($request->param());

        $conflict = $model->where([
            ['term_id', '=', $courseModel->term_id],
            ['week', '=', $courseModel->week],
            ['room', '=', $courseModel->room],
            ['', 'exp', Db::raw('(class_week&' . $courseModel->class_week . ')<>0 and (class_time&' . $courseModel->class_time . ')<>0')],
            ['id', '<>', $id],
        ])->column('id');

        if ($conflict) {
            return json_response($conflict, STATUS_INVALD_PARAM, '新传入的数据与原始数据存在冲突,data中存储了与此提交冲突的所有课程的id');
        }
        $courseModel->isUpdate(true)->save();
        return json_response([]);
    }

    /**
     * 批量保存课程信息
     *
     * @return void
     */
    public function saveList()
    {
        $this->checkPermission(1);
        return parent::_saveList();
    }

    /**
     * 获取所有教师的数据
     *
     * @return void
     */
    public function getTeacherNames()
    {
        $this->checkPermission(10);
        return json_response(db('course_room')->whereExp('teacher', 'is not null')->distinct(true)->column('teacher'));
    }

    /**
     * 获取所有上课班级的数据
     *
     * @return void
     */
    public function getClassNames()
    {
        $this->checkPermission(10);
        return json_response(db('course_room')->whereExp('class_name', 'is not null')->distinct(true)->column('class_name'));
    }

    /**
     * 排课建议查询
     *
     * @return void
     */
    public function scheduleAdvise()
    {
        $this->checkPermission(10);
        $request = $this->request;
        if (!($termId = $request->param('term_id'))) {
            return json_response([], STATUS_INVALD_PARAM, "必须传递term_id参数");
        }
        if (!($week = $request->param('week'))) {
            return json_response([], STATUS_INVALD_PARAM, "必须传递week参数");
        }
        if (!($classWeek = $request->param('class_week'))) {
            return json_response([], STATUS_INVALD_PARAM, "必须传递class_week参数");
        }
        if (!($classTime = $request->param('class_time'))) {
            return json_response([], STATUS_INVALD_PARAM, "必须传递class_time参数");
        }

        return json_response(CourseRoom::scheduleAdvise($termId,$week,$classWeek,$classTime,$request->param('building')));
    }

}
